package gui;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import evolutionaryProblems.Problem;

public class GUIFrame extends JFrame{

	private static final long serialVersionUID = 1L;
	JPanel contentPane;
	
	public GUIFrame(final Problem problem){
		super("Evolutionary Algorithm"); 
		initContentPane(); 
		setContentPane(contentPane); 
		setSize(1000, 800); 
		addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				problem.killSimulation(); 
			}
			
		}); 
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setVisible(true); 
	}

	private void initContentPane() {
		contentPane = new JPanel(); 
		contentPane.setLayout(new GridBagLayout()); 
	}
	
	public void addSettingPanel(JPanel settingPanel){
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.gridx = 0; 
		constraints.gridy = 0; 
		contentPane.add(settingPanel,constraints); 
	}
	
	public void addProblemPanel(JPanel problemPanel){
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.gridx = 0; 
		constraints.gridy = 0; 
		contentPane.add(problemPanel,constraints); 
	}
	
	public void addLineChart(JPanel lineChartPanel){
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.gridx = 0; 
		constraints.gridy = 2; 
		contentPane.add(lineChartPanel, constraints); 
	}
	
	public void addGamePanel(JPanel gamePanel){
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.gridx = 1; 
		constraints.gridy = 1; 
		constraints.gridheight = 2; 
		contentPane.add(gamePanel, constraints);
	}
	
	@Override
	public void paint(Graphics g){
		Graphics offgc;
	    Image offscreen = null;
	    int width = getWidth(); 
	    int height = getHeight(); 

	    // create the offscreen buffer and associated Graphics
	    offscreen = createImage(width, height);
	    offgc = offscreen.getGraphics();
	    // clear the exposed area
	    offgc.setColor(getBackground());
	    offgc.fillRect(0, 0, width, height);
	    offgc.setColor(getForeground());
	    // do normal redraw
	    super.paint(offgc);
	    // transfer offscreen to window
	    g.drawImage(offscreen, 0, 0, this);
	}
	
	
}
